package com.cbw.express;

import java.util.Scanner;
public class Express {
    static Scanner sc = new Scanner(System.in);
    static String[] numbers = new String[10]; //单号
    static String[] companies = new String[10];//公司名称
    static int[] codes = new int[10];//取件码
    static int count = 0;//总数量：

    public static void main(String[] args) {
        //流程：while+switch 嵌套方式
        boolean flag = true;
        while (flag) {
            System.out.println("请选择用户：1.快递员，2.用户，3.退出");
            String key = sc.next();
            switch (key) {
                case "1":
                    adminLoop();
                    break;
                case "2":
                    userLoop();
                    break;
                case "3":
                    flag = false;
                    System.out.println("再见！");
                    break;
                default:
                    System.out.println("输入错误，请检查!");
                    break;
            }
        }
    }

    /**
     * 用户
     */
    private static void userLoop() {

        boolean flag = true;
        while (flag) {
            System.out.println("请选择 1.取件 2.返回主菜单!");
            String key = sc.next();
            switch (key) {
                case "1":
                    pickExpress();
                    break;
                case "2":
                    flag = false;
                    System.out.println("返回主菜单!");
                    break;

                default:
                    System.out.println("输入错误，请检查!");
                    break;
            }
        }
    }

    /**
     * 取件
     */
    private static void pickExpress() {
        int code=inputCode();
        int index = findCodeBy(code);
        if (index==-1){
            System.out.println("无此快递！");
        }else{
            removeByIndex(index);
            System.out.println("取件成功！");
        }
    }

    private static int inputCode() {
        System.out.println("请输入取件码：");
        return sc.nextInt();
    }

    /**
     * 快递员
     */
    private static void adminLoop() {
        boolean flag = true;
        while (flag) {
            System.out.println("请选择：1.增加快递，2.删除快递 3.修改快递 4.查看所有 5.返回主菜单!");
            String key = sc.next();
            switch (key) {
                case "1":
                    addExpress();
                    break;
                case "2":
                    removeExpress();
                    break;
                case "3":
                    modifyExpress();
                    break;
                case "4":
                    showAllExpresses();
                    break;
                case "5":
                    flag = false;
                    System.out.println("返回主菜单!");
                    break;
                default:
                    System.out.println("输入错误，请检查!");
                    break;
            }
        }
    }

    /**
     * 添加快递
     */
    private static void addExpress() {
        //1.找一个空位置：
        int empty = findEmpty();
        //2.准备快递信息
        if (empty < 0) {
            System.out.println("快递柜已满，没有空位可以存储！");
        } else {
            String number = inputNumber();
            if (findExpressByNumber(number) < 0) {
                String company = inputCompany();
                int code = initCode();
                saveExpress(number, company, code, empty);
                System.out.println("存储快递成功，取件码为：" + code);
            } else {
                System.out.println("单号重复，无法添加！");
            }

        }
        //单号查重
        //取件码查重
        //3.填进去
    }

    private static void saveExpress(String number, String company, int code, int index) {
        numbers[index] = number;
        companies[index] = company;
        codes[index] = code;
        count++;
    }


    /**
     * 生成不重复的取件码:
     * @return
     */
    private static int initCode() {
        int code = 0;
        //先生成一个，然后去判断有没有，没有，则可以用，有，则重复生成
        do {
            code = getCode();
        } while (findCodeBy(code) > 0);

        return code;
    }

    /**
     * 查重
     *
     * @param code
     * @return
     */
    private static int findCodeBy(int code) {
        for (int i = 0; i < codes.length; i++) {
            if (codes[i] == code) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 随机一个指定范围的取件码：
     * random的范围【0,1）的浮点数
     * @return
     */
    private static int getCode() {
        return (int) (Math.random() * 900 + 100);
    }

    /**
     * 通过单号找快递
     * @param number
     * @return
     */
    private static int findExpressByNumber(String number) {

        for (int i = 0; i < numbers.length; i++) {
            if (number.equals(numbers[i])) {
                return i;
            }
        }
        return -1;
    }

    private static String inputCompany() {
        System.out.println("请输入公司名称：");
        return sc.next();
    }

    private static String inputNumber() {
        System.out.println("请输入快递单号：");
        return sc.next();
    }

    /**
     * 查找一个空位，用来存储快递
     *
     * @return 如果是-1 证明没有空位
     */
    private static int findEmpty() {
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == null) {
                return i;
            }
        }
        return -1;
    }

    private static void removeExpress() {
        //按照单号删除的
        String number = inputNumber();
        int index = findExpressByNumber(number);
        if (index == -1) {
            System.out.println("查无此单号，删除失败！");
        } else {
            System.out.println("是否确认删除？输入y删除，其他取消操作：");
            if (sc.next().equals("y")) {
                removeByIndex(index);
                System.out.println("删除成功!");
            } else {
                System.out.println("用户取消操作!返回菜单!");
            }
        }
    }

    /**
     * 把指定的下标下的三个数组的对应值置为默认值
     * @param index
     */
    private static void removeByIndex(int index) {
        numbers[index] = null;
        companies[index] = null;
        codes[index] = 0;
        count--;
    }

    private static void modifyExpress() {
        String number=inputNumber();
        int index = findExpressByNumber(number);
        if (index>=0){
            //旧的移除
            removeByIndex(index);
            String newNumber=inputNewNumber();
            if (findExpressByNumber(newNumber)<0){
                String newCompany=inputNewCompany();
                int newCode = initCode();
                saveExpress(newNumber,newCompany,newCode,index);
                System.out.println("修改快递信息成功!");
            }else{
                System.out.println("新单号重复，不能使用！");
            }
        }else{
            System.out.println("查无此单号，修改失败!");
        }
    }

    private static String inputNewNumber() {
        System.out.println("请输入新的快递单号：");
        return sc.next();
    }

    private static String inputNewCompany() {
        System.out.println("请输入新的快递公司名称：");
        return sc.next();
    }

    /**
     * 查询所有
     */
    private static void showAllExpresses() {
        if (count == 0) {
            System.out.println("无任何快递信息！");
            return;
        }
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] != null) {
                System.out.println("单号：" + numbers[i] + "\t公司名称：" + companies[i] + "取件码\t" + codes[i]);
            }
        }
    }
}